package com.fast.develop.configuration;

import com.fast.develop.configuration.persistence.ResourceFileEntity;
import com.fast.develop.configuration.properties.FastDevelopPropesties;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.domain.EntityScanPackages;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 项 目 名：gs-tto-builder-parent
 * 源文件名：com.geekstorm.tto.builder.engine.DefaultBuilderEngineConfiguration
 * 文件版本：1.0.0
 * 创建人： 莫寅
 * 创建日期：2020-06-07 00:31
 * 修改作者：莫寅BuilderEngineWebRegistrar
 * 修改日期：2020-06-07 00:31
 * 文件描述：
 * By：2020 © CompanyName  , Inc. All Rights Reserved.
 */
@Slf4j
@Getter
@Configuration
public class FastJpaRepositoryConfiguration implements BeanFactoryAware {


    @Autowired
    private JpaProperties jpaProperties;
    @Autowired
    private HibernateProperties hibernateProperties;
    @Autowired
    private DataSource dataSource;
    @Autowired
    private FastDevelopPropesties fastDevelopPropesties;

//    @Autowired
//    private PersistenceProperties persistenceProperties;
//
//
    private ConfigurableListableBeanFactory beanFactory;


    protected String[] getPackagesToScan() {
        List<String> packages = EntityScanPackages.get(this.beanFactory).getPackageNames();
        if (packages.isEmpty() && AutoConfigurationPackages.has(this.beanFactory)) {
            packages = AutoConfigurationPackages.get(this.beanFactory);
        }

        List<String> newPackages = new ArrayList<>(packages);
        newPackages.add("com.geekstorm.titan.framework.business");
        return StringUtils.toStringArray(newPackages);
    }



    public Map<String, Object> getVendorProperties() {
        Map map = jpaProperties.getProperties();

//        map.put("hibernate.default_schema", persistenceProperties.getDefaultSchema());
//        map.put("hibernate.session_factory.statement_inspector", persistenceProperties.getStatementInspector());
//        map.put("hibernate.ejb.interceptor", persistenceProperties.getEjbInterceptor());

        if(fastDevelopPropesties.getDomain().getDdlAuto() != null){
            hibernateProperties.setDdlAuto(fastDevelopPropesties.getDomain().getDdlAuto().getValue());
        }
//        hibernateProperties.getNaming().setPhysicalStrategy(persistenceProperties.getNamingPhysicalStrategy());


        jpaProperties.setShowSql(fastDevelopPropesties.getDomain().isShowSql());
        jpaProperties.setGenerateDdl(true);

        return hibernateProperties.determineHibernateProperties(map, new HibernateSettings());
    }


    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(dataSource)
                .properties(getVendorProperties())
//                .packages(getPackagesToScan())
                .persistenceUnit("builderEngineRepository")
                .packages(ResourceFileEntity.class)
                .build();
    }

    @Bean(name = "transactionManager")
    PlatformTransactionManager transactionManagerPrimary(EntityManagerFactory builder) {
        return new JpaTransactionManager(builder);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
    }

//
//
//    @Bean
//    @ConditionalOnMissingBean
//    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
//        JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(entityManager);
//        return jpaQueryFactory;
//    }
//
//    @Bean
//    @ConditionalOnMissingBean
//    public JPAQueryWrapper jpaQueryWrapper() {
//        JPAQueryWrapper jpaQueryWrapper = new JPAQueryWrapper();
//        return jpaQueryWrapper;
//    }
}
